/**
 * @author KujoMiva
 * @name Router.ts
 * @desc 
 * @time 2020年02月08日 18:50:16 星期六
 */
class Router extends egret.DisplayObjectContainer {
  private animationSpeed: number = 800
  public constructor() {
    super()
    this.once(egret.Event.ADDED_TO_STAGE, this.init, this)
  }

  private init() {
    this.runStation()
    this.createBtnNextPage()
    this.createSwitchPageAnimation()
    this.createTags()
    this.createMusic()
    this.stage.dispatchEvent(new egret.Event('gotoHome'))
    new MivaEgret()
    $('.item-mask').hide()
  }

  private createMusic() {
    const music = new Music()
    this.addChild(music)

    this.addEventListener('showMask', () => {
      music.createMask()
      $('.item-mask').show()
    }, this)
  }

  private createTip() {
    if (ParamsPool.NOW_PAGE_INDEX == 1) {
      const tip = Utils.createBitmapByName('down_png')
      this.addChild(tip)
      Utils.anchorCenter(tip)
      tip.x = this.stage.stageWidth / 2
      tip.y = this.stage.stageHeight / 2

      tip.touchEnabled = true
      tip.addEventListener(egret.TouchEvent.TOUCH_BEGIN, () => {
        this.removeChild(tip)
      }, this)


    }
  }

  private runStation() {
    this.stage.addEventListener('gotoHome', () => {
      const screen = new HomeScreen()
      this.addChild(screen)
    }, this)

    this.stage.addEventListener('gotoPageFirst', () => {
      const screen = new PageFirstScreen()
      this.addChild(screen)
      // 打开的📖右滑到了指定位置, 显示翻页按钮
      screen.once('pageShowOver', () => {
        this.dispatchEvent(new egret.Event('btnNextPageShow'))
        // this.dispatchEvent(new egret.Event('showTags'))
      }, this)
      // 点击了翻页按钮,隐藏📖的内容
      this.once('btnNextPageTap', () => {
        screen.hideContent()
      }, this)
      // 📖内容隐藏完毕之后进行翻页动画
      screen.addEventListener('pageHideOver', () => {
        this.stage.dispatchEvent(new egret.Event('switch_page_start'))
      }, this)
      // 翻页动画执行完毕后向舞台分发跳转到空白页的事件并删除掉📖第一页
      this.once('switchPageOver', () => {
        ParamsPool.NOW_PAGE_INDEX = 0
        this.stage.dispatchEvent(new egret.Event('gotoPageBlank'))
        this.removeChild(screen)
      }, this)
    }, this)

    this.stage.addEventListener('gotoPageBlank', () => {
      if (ParamsPool.NOW_PAGE_INDEX == 0) {
        this.dispatchEvent(new egret.Event('showMask'))
      }
      const screen = new PageBlankScreen()
      this.addChild(screen)
      this.dispatchEvent(new egret.Event('btnNextPageShow'))
      this.dispatchEvent(new egret.Event('showTags'))
      // 监听翻页按钮点击事件,执行隐藏📖内容方法
      this.once('btnNextPageTap', () => {
        // ParamsPool.NOW_PAGE_INDEX++
        screen.hideContent()
      }, this)
      // 📖内容隐藏完毕之后进行翻页动画
      screen.addEventListener('pageHideOver', () => {
        this.stage.dispatchEvent(new egret.Event('switch_page_start'))
      }, this)
      // 翻页动画执行完毕后向舞台分发跳转到空白页的事件并删除掉📖这一页
      this.once('switchPageOver', () => {
        this.stage.dispatchEvent(new egret.Event('gotoPageBlank'))
        this.removeChild(screen)
        this.createTip()
      }, this)
      this.stage.once('tagTap', () => {
        screen.hideContent()
      }, this)
    }, this)
  }

  private createBtnNextPage() {
    const btn = Utils.createBitmapByName('btn_next_page_png')
    this.addChild(btn)
    const btnX = 520
    btn.x = btnX + 100
    btn.y = 770
    btn.alpha = 0
    btn.touchEnabled = true
    // 点击翻页按钮时先执行隐藏按钮动画,按钮隐藏完毕后向父容器分发翻页按钮点击事件
    btn.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
      egret.Tween.get(btn)
        .to({ x: btnX + 100, alpha: 0 }, 300)
        .call(() => {
          ParamsPool.NOW_PAGE_INDEX++
          this.dispatchEvent(new egret.Event('btnNextPageTap'))
        })
    }, this)
    // 监听父容器派发的翻页按钮显示事件
    this.addEventListener('btnNextPageShow', () => {
      btn.parent.sortableChildren = true
      btn.zIndex = 99
      const nowDay = DayData.data[ParamsPool.NOW_PAGE_INDEX].day
      if (nowDay !== '尾声') {
        egret.Tween.get(btn).to({ x: btnX, alpha: 1 }, 300)
      }
    }, this)

    this.addEventListener(egret.Event.ENTER_FRAME, () => {
      const nowDay = DayData.data[ParamsPool.NOW_PAGE_INDEX].day
      if (nowDay === '尾声') {
        btn.alpha = 0
        $('.item-btn-back').show()
      }
    }, this)
  }

  private createSwitchPageAnimation() {
    let num = 0
    const turn = Utils.createBitmapByName(`turn_1_png`)
    this.addChild(turn)
    Utils.anchorCenter(turn)
    turn.x = this.stage.stageWidth / 2
    turn.y = this.stage.stageHeight / 2
    turn.alpha = 0

    const timer: egret.Timer = new egret.Timer(200, 5)
    timer.addEventListener(egret.TimerEvent.TIMER, () => {
      num++
      turn.alpha = 1
      turn.texture = RES.getRes(`turn_${num}_png`)
      turn.zIndex = 99
      if (num > 4) {
        num = 0
      }
    }, this)

    timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, () => {
      turn.alpha = 0
      this.dispatchEvent(new egret.Event('switchPageOver'))
    }, this)

    this.stage.addEventListener('switch_page_start', () => {
      num = 0
      timer.reset()
      timer.start()
    }, this)
  }

  private createTags() {
    const tags = new Tags()
    this.addChild(tags)
    tags.x = 10
    tags.y = 940
    this.sortableChildren = true
    tags.zIndex = 95
    tags.alpha = 0

    this.addEventListener('showTags', () => {
      tags.alpha = 1
    }, this)
  }
}